package com.pg.agent.engine.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pg.agent.engine.client.message.OutPutMessage;
import com.pg.agent.engine.client.vo.SOPResultVO;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;

@Slf4j
public class Utils {


    public static void outputStreamResult(String content, String dataId, Boolean finish, OutPutMessage data, HttpServletResponse response) {

        SOPResultVO instance = SOPResultVO.instance(content, finish, dataId, data);
        if (data.isFirst()){
            data.setFirst(false);
            instance.setFirst(1);
        }
        log.info("sop run end out：SOPResultVO={}", JSON.toJSONString(instance));
        try {
            HttpClientUtils.sendSseEventStream(JSONObject.toJSONString(instance), response);
        } catch (Exception e) {
            log.info("异常：{}", e.getMessage());
        }
    }


    public static void outputStreamResult(int code, String msg, Boolean finish, String dataId, HttpServletResponse response) {

        SOPResultVO instance = SOPResultVO.instance(code, msg, finish, dataId);
        instance.setFirst(1);

        try {
            HttpClientUtils.sendSseEventStream(JSONObject.toJSONString(instance), response);
        } catch (Exception e) {
            log.info("异常：{}", e.getMessage());
        }
    }
    public static void outputStreamResult(int code, String msg, Boolean finish, String dataId, OutPutMessage data, HttpServletResponse response) {

        SOPResultVO instance = SOPResultVO.instance(code, msg, finish, dataId);
        if (data.isFirst()){
            data.setFirst(false);
            instance.setFirst(1);
        }
        instance.setData(data);
        try {
            HttpClientUtils.sendSseEventStream(JSONObject.toJSONString(instance), response);
        } catch (Exception e) {
            log.info("异常：{}", e.getMessage());
        }
    }
}
